//
//  DateStrings.m
//  Reminders
//
//  Created by Nearsoft User on 8/29/12.
//
//

#import "DateStrings.h"

@implementation DateStrings

#pragma mark - Date Methods
- (NSString *)fullStringFromDate:(NSDate *)date
{
    return [NSString stringWithFormat:@"%@ at %@", [self stringFromDay:date], [self stringFromTime:date]];
}

- (NSDate *)stringToDate:(NSString *)date
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    return [dateFormat dateFromString:date];
}

- (NSString *)stringFromDate:(NSDate *)date
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    return [dateFormat stringFromDate:date];
}
- (NSString *)stringFromDay:(NSDate *)day
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"LLLL d, yyyy"];

    return [dateFormat stringFromDate:day];
}

- (NSDate *)stringToDay:(NSString *)day
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"LLLL d, yyyy"];

    return [dateFormat dateFromString:day];
}

- (NSString *)stringFromTime:(NSDate *)time
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"h:mm a"];

    return [dateFormat stringFromDate:time];
}

- (NSDate *)stringToTime:(NSString *)time
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

    [dateFormat setDateFormat:@"h:mm a"];

    NSDateComponents *todayComponents = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:[NSDate date]];

    NSDateComponents *timeComponents = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit) fromDate:[dateFormat dateFromString:time]];

    timeComponents.year = todayComponents.year;
    timeComponents.month = todayComponents.month;
    timeComponents.day = todayComponents.day;

    return [[NSCalendar currentCalendar] dateFromComponents:timeComponents];
}

- (NSString *)getDayString:(NSDate *)date
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd"];
    
    return [dateFormat stringFromDate:date];
}

- (NSString *)getMonthString:(NSDate *)date
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"LLL"];
    
    return [dateFormat stringFromDate:date];
}

- (NSString *)getYearString:(NSDate *)date
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy"];
    
    return [dateFormat stringFromDate:date];
}

- (NSDate *)parseDates:(NSDate *)day time:(NSDate *)time
{
    NSDateComponents *dayComponents = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:day];
    NSDateComponents *timeComponents = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit|NSMinuteCalendarUnit) fromDate:time];
    
    NSDateComponents *combinedDate =[[NSDateComponents alloc] init];
    combinedDate.year = dayComponents.year;
    combinedDate.month = dayComponents.month;
    combinedDate.day = dayComponents.day;
    combinedDate.hour = timeComponents.hour;
    combinedDate.minute = timeComponents.minute;

    return [[NSCalendar currentCalendar] dateFromComponents:combinedDate];
}

- (NSDateComponents *)getDateComponents:(NSDate *)date
{
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:[NSDate date]];

    return components;
}

- (Boolean)dateIsOlderThanToday:(NSDate *)date
{
    // Create a date from the current calendar
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit) fromDate:[NSDate date]];
    NSDate *today = [[NSCalendar currentCalendar] dateFromComponents:components];

    // Create a date from the date parameter
    components = [[NSCalendar currentCalendar] components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit) fromDate:date];
    NSDate *otherDate = [[NSCalendar currentCalendar] dateFromComponents:components];

    // If the date parameter older than the current day
    if([otherDate compare:today] != NSOrderedDescending)
        return YES;
    else
        return NO;
}

- (NSDate *)addDate:(NSInteger)repeat date:(NSDate *)date
{
    NSDateComponents *components =[[NSDateComponents alloc] init];

    switch (repeat)
    {
                // Every Day
        case 1: components.day = 1;
                break;
                // Every Week
        case 2: components.day = 7;
                break;
                // Every Month
        case 3: components.month = 1;
                break;
    }

    return [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:date options:0];
}

@end